//
//  LXGiftContainerView.swift
//  礼物动画展示
//
//  Created by seven on 2017/12/29.
//  Copyright © 2017年 seven. All rights reserved.
//

import UIKit

private let KChannelCount = 2
private let KChannelViewH: CGFloat = 40
private let KChannelMargin: CGFloat = 10

class LXGiftContainerView: UIView {

    private lazy var channelViews : [LXChannelView] = [LXChannelView]()
    private lazy var cacheGiftModels: [LXGiftModel] = [LXGiftModel]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK:- 设置UI
extension LXGiftContainerView{
    private func setupUI(){
        //1.根据当前渠道数创建LXChannelView
        let w: CGFloat = frame.width
        let h: CGFloat = KChannelViewH
        let x: CGFloat = 0
        for i in 0..<KChannelCount{
            let y: CGFloat = (h + KChannelMargin) * CGFloat(i)
            let channelView = LXChannelView.loadFromNib()
            channelView.frame = CGRect(x: x, y: y, width: w, height: h)
            channelView.alpha = 0
            addSubview(channelView)
            channelViews.append(channelView)
            
            channelView.complectionCallback = { channelview in
                //1.取出缓存中的模型
                if self.cacheGiftModels.count != 0 {
                    return
                }
                
                //2.取出缓存的第一个模型
                let fisetGiftModel = self.cacheGiftModels.first!
                self.cacheGiftModels.removeFirst()
                
                //3.让闲置的view执行动画
                channelView.giftModel = fisetGiftModel
                
//                for i in (0..<self.cacheGiftModels.count).reversed(){
//                    let giftModel = self.cacheGiftModels[i]
//                    if giftModel.isEqual(fisetGiftModel){
//                        channelView.addOnceToCache()
//                        self.cacheGiftModels.remove(at: i)
//                    }
//                }
                
            }
        }
    }
}

extension LXGiftContainerView {
    func showGiftModel(giftModel: LXGiftModel){
        //0.判断正在忙的channelView和赠送新礼物的（username/giftname）
        if let channelView = checkUsingChannelView(giftModel: giftModel) {
            channelView.addOnceToCache()
            return
        }
        //1.判断是否有闲置的channelView
        if let channelView = checkIdelChanelView() {
            channelView.giftModel = giftModel
            return
        }
        //2.将数据放入缓存
        cacheGiftModels.append(giftModel)
        print(cacheGiftModels.count)
    }
    
    private func checkUsingChannelView(giftModel: LXGiftModel) -> LXChannelView?{
        for channelView in channelViews{
            if giftModel.isEqual(channelView.giftModel) && channelView.state != .endingAnimating{
                return channelView
            }
        }
        return nil
    }
    
    private func checkIdelChanelView() -> LXChannelView?{
        for channelView in channelViews{
            if channelView.state == .idel{
                return channelView
            }
        }
        return nil
    }
}
